//
//  CustomButton.m
//  练习_11.03_1
//
//  Created by rimi1 on 2016/11/3.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "WXQButton.h"

@interface WXQButton ()

@property (nonatomic, strong) UIImageView *wBackImageView;
@property (nonatomic, strong) UIImageView *wImageView;
@property (nonatomic, strong) UILabel *wTitle;

// 存储各种状态下的数据
@property (nonatomic, strong) UIImage *backImageNormal;
@property (nonatomic, strong) UIImage *backImageSelected;

@property (nonatomic, copy) NSString *titleNormal;
@property (nonatomic, copy) NSString *titleSelected;
@property (nonatomic, strong) UIColor *titleColorNormal;
@property (nonatomic, strong) UIColor *titleCcolorSelected;

@property (nonatomic, strong) UIImage *imageNormal;
@property (nonatomic, strong) UIImage *imageSelected;

@end

@implementation WXQButton

#pragma mark - initialize
// !!!!! 注意：当用 self. 才会触发set方法，直接 _xxx 是不会走set方法的
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor imageName:(NSString *)imageName imageLocation:(WXQButtonImageLocation)imageLocation {
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitle:title forState:UIControlStateNormal];
        if (titleColor) {
            [self setTitleColor:titleColor forState:UIControlStateNormal];
        }
        [self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        // 写在set方法里面了
        self.imageLocation = imageLocation;
    }
    return self;
}

#pragma mark - 居中
// y轴居中
- (void)inTheMidY {
    CGRect rect = self.wImageView.frame;
    rect.origin.y = (self.bounds.size.height - self.wImageView.bounds.size.height)/2;
    self.wImageView.frame = rect;
}
// x轴居中
- (void)inTheMidX {
    CGRect rect = self.wImageView.frame;
    rect.origin.x = (self.bounds.size.width - self.wImageView.bounds.size.width)/2;
    self.wImageView.frame = rect;
}

#pragma mark - Method set

- (void)setBackImage:(UIImage *)backImage forState:(UIControlState)state {
    // 当设置图片时才会初始化backImageView(内存优化吧，个人认为)，并且把 backImageView 放在最底层
    [self insertSubview:self.wBackImageView atIndex:0];
    // 根据不同的状态给不同的image赋值
    switch (state) {
        case UIControlStateNormal:{
            _backImageNormal = backImage;
        }
            break;
            
        case UIControlStateSelected:{
            _backImageSelected = backImage;
        }
            break;
            
        default:
            break;
    }
    
    // 刷新界面
    self.wStatus = self.wStatus;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    [self addSubview:self.wTitle];
    switch (state) {
        case UIControlStateNormal:{
            _titleNormal = title;
        }
            break;
            
        case UIControlStateSelected:{
            _titleSelected = title;
        }
            break;
            
        default:
            break;
    }
    self.wStatus = self.wStatus;
}

- (void)setTitleColor:(UIColor *)titleColor forState:(UIControlState)state {
    switch (state) {
        case UIControlStateNormal:{
            _titleColorNormal = titleColor;
        }
            break;
            
        case UIControlStateSelected:{
            _titleCcolorSelected = titleColor;
        }
            break;
            
        default:
            break;
    }
    self.wStatus = self.wStatus;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    [self addSubview:self.wImageView];
    switch (state) {
        case UIControlStateNormal:{
            _imageNormal = image;
        }
            break;
            
        case UIControlStateSelected:{
            _imageSelected = image;
        }
            break;
            
        default:
            break;
    }
    self.wStatus = self.wStatus;
}

#pragma mark - set

- (void)setWSelect:(BOOL)wSelect {
    if (_wSelect != wSelect) {
        _wSelect = wSelect;
        // 通过不同的BOOL值给按钮不同的状态(注意：得用self，然后再自动调用status的set方法)
        if (_wSelect) {
            self.wStatus = UIControlStateSelected;
        }else {
            self.wStatus = UIControlStateNormal;
        }
    }
}

- (void)setWStatus:(UIControlState)wStatus {
    // 根据不同状态符不同的值(这里没设if，是因为方便set给值时，刷新界面)
    _wStatus = wStatus;
    switch (_wStatus) {
        case UIControlStateNormal:{
            _wBackImageView.image = _backImageNormal;
            _wTitle.text = _titleNormal;
            _wTitle.textColor = _titleColorNormal;
            _wImageView.image = _imageNormal;
        }
            break;
            
        case UIControlStateSelected:{
            // 当选中状态的数据有就赋值，没有就什么都不做
            if (_backImageSelected) {
                _wBackImageView.image = _backImageSelected;
            }
            if (_titleSelected) {
                _wTitle.text = _titleSelected;
            }
            if (_titleCcolorSelected) {
                _wTitle.textColor = _titleCcolorSelected;
            }
            if (_imageSelected) {
                _wImageView.image = _imageSelected;
            }
            
        }
            break;
            
        default: {
            // 默认保险~好像也没什么用
            _wBackImageView.image = _backImageNormal;
            _wTitle.text = _titleNormal;
            _wTitle.textColor = _titleColorNormal;
            _wImageView.image = _imageNormal;
        }
            break;
    }
}

- (void)setImageLocation:(WXQButtonImageLocation)imageLocation {
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    if (_imageLocation != imageLocation) {
        _imageLocation = imageLocation;
        switch (_imageLocation) {
            case WXQImageLocationLeft:{
                self.wImageView.frame = CGRectMake(0, 0, height, height);
                self.wTitle.frame = CGRectMake(CGRectGetMaxX(self.wImageView.frame), 0, width - CGRectGetWidth(self.wImageView.bounds), height);
                // 左右，y轴居中，这样子，如果有什么改变就直接进来改图片大小和间距就可以了
                [self inTheMidY];
            }
                break;
                
            case WXQImageLocationTop:{
                self.wImageView.frame = CGRectMake(0, 0, 45, 45);
                self.wTitle.frame = CGRectMake(0, CGRectGetMaxY(self.wImageView.frame), width, height - CGRectGetHeight(self.wImageView.bounds));
                // 上下，x轴居中
                [self inTheMidX];
            }
                break;
                
            case WXQImageLocationRight:{
                self.wImageView.frame = CGRectMake(width - height, 0, height, height);
                self.wTitle.frame = CGRectMake(0, 0, width - CGRectGetWidth(self.wImageView.bounds), height);
                [self inTheMidY];
            }
                break;
                
            case WXQImageLocationBottom:{
                self.wImageView.frame = CGRectMake(0, height - width, width, width);
                self.wTitle.frame = CGRectMake(0, 0, width, height - CGRectGetHeight(self.wImageView.bounds));
                [self inTheMidX];
            }
                break;
                
            default:{
                self.imageLocation = WXQImageLocationLeft;
            }
                break;
        }
    }
}

#pragma mark - get

- (UIImageView *)wBackImageView {
    
    if (!_wBackImageView) {
        
        _wBackImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        
    }
    return _wBackImageView;
}

- (UIImageView *)wImageView {
    
    if (!_wImageView) {
        // 默认在左边的值
        _wImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height)];
        
    }
    return _wImageView;
}

- (UILabel *)wTitle {
    
    if (!_wTitle) {
        
        _wTitle = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.wImageView.frame) + 3, 0, self.bounds.size.width - self.bounds.size.height, self.bounds.size.height)];
        
    }
    return _wTitle;
}



@end














